package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import com.sinosoft.hydra.um.LoginUser;
import com.sinosoft.hydra.um.condition.SearchProcessTemplateCondition;
import com.sinosoft.hydra.um.constant.BaseConstant;
import com.sinosoft.hydra.um.domain.model.DirectoryType;
import com.sinosoft.hydra.um.domain.model.LabDataParser;
import com.sinosoft.hydra.um.domain.permission.*;
import cn.com.sinux.spring.exception.BusinessException;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.ProcessManageService;
import cn.com.sinux.spring.utils.PageUtils;
import cn.com.sinux.spring.json.JsonFilter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
@Controller
@RequestMapping("/api/process")
public class SysProcessApi {

    @Autowired
    private ProcessManageService processManageService;

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("index")
    @RequiresPermissions("process:data:list")
    public String index(Model model) {
        LoggerHelper.logInfo("访问了模板列表信息");
        return "hydra_easyui/process/index";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toAdd")
    @RequiresPermissions("process:data:add")
    public String toAdd(Model model) {
        model.addAttribute("directoryTypes", DirectoryType.values());
        return "hydra_easyui/process/add";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toUpdate")
    @RequiresPermissions("process:data:update")
    public String toUpdate(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("process", processManageService.findOne(id));
        model.addAttribute("directoryTypes", DirectoryType.values());
        return "hydra_easyui/process/update";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("toSearch")
    @RequiresPermissions("process:data:list")
    public String search(Model model) {
        return "hydra_easyui/process/search";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @RequiresPermissions("process:data:add")
    @ResponseBody
    public Object add(Model model, LoginUser loginUser, TProcessTemplate processTemplate) {
        processTemplate.setCreateUser(loginUser.getUserName());
        TProcessTemplate result = processManageService.addProcess(processTemplate);
        LoggerHelper.logInfo("创建了工艺模板{}", result.getProcessName());
        return JsonFilter.doPropertyFilter(result, new PropertyCollection()
                .addProperties(TProcessTemplate.class, "id", "processName"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @RequiresPermissions("process:data:update")
    @ResponseBody
    public Object update(Model model, LoginUser loginUser, TProcessTemplate processTemplate) {
        processTemplate.setUpdateUser(loginUser.getUserName());
        TProcessTemplate result = processManageService.updateProcess(processTemplate);
        LoggerHelper.logInfo("修改了工艺模板{}", processTemplate.getProcessName());
        return JsonFilter.doPropertyFilter(result, new PropertyCollection()
                .addProperties(TProcessTemplate.class, "id", "processName"));
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("detail")
    @RequiresPermissions("process:data:detail")
    public String preview(Model model, @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("process", processManageService.findOne(id));
        return "hydra_easyui/process/detail";
    }

    /**
     * 页面跳转
     *
     * @return 跳转制定也
     */
    @RequestMapping("gotoParse")
    @RequiresPermissions("process:data:parse")
    public String gotoParse(Model model,
                            @RequestParam(value = "code", required = true) String code) {
        LabDataParser labDataParser = LabDataParser.valueOf(code);
        if (labDataParser == null) {
            throw new BusinessException("解析页不存在，请重试");
        }
        model.addAttribute("labels", labDataParser.getLabels());
        model.addAttribute("code", labDataParser.getCode());
        model.addAttribute("title", labDataParser.getTitle());
        return labDataParser.getParsePage();
    }


    /**
     * 查询工艺模板
     *
     * @param searchProcessTemplateCondition
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findProcessTemplate")
    @RequiresPermissions(value = "process:data:list")
    @ResponseBody
    public Object updateDirectory(
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer pageNumber,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
            SearchProcessTemplateCondition searchProcessTemplateCondition) throws Exception {
        Pageable page = PageUtils.buildPageRequest(pageNumber, pageSize, "id", "desc");
        searchProcessTemplateCondition.setLikeName(true);
        Page<TProcessTemplate> processTemplates = processManageService.findAll(searchProcessTemplateCondition, page);
        return JsonFilter.doPropertyFilter(processTemplates, new PropertyCollection()
                .addProperties("id", "processName", "module", "createUser", "createTime", "useAble", "remark", "processCover",
                        "useDevice", "device")
                .addProperties(TDirectory.class, "id", "dirValue")
                .addProperties(SysUserMember.class, "userName")
                .addProperties(TDevice.class, "deviceName"));
    }

    /**
     * 查询工艺模板
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "list")
    @RequiresPermissions(value = "process:data:list")
    @ResponseBody
    public Object list() throws Exception {
        SearchProcessTemplateCondition searchProcessTemplateCondition = new SearchProcessTemplateCondition();
        searchProcessTemplateCondition.setUseAble(BaseConstant.INTEGER_TRUE);
        List<TProcessTemplate> processTemplates = processManageService.findAll(searchProcessTemplateCondition, null).getContent();
        return JsonFilter.doPropertyFilter(processTemplates, new PropertyCollection()
                .addProperties(TProcessTemplate.class, "id", "processName", "module", "processCover", "searchAble")
                .addProperties(TDirectory.class, "id", "dirValue"));
    }


    /**
     * 查询工艺模板
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "searchAttributes")
    @RequiresPermissions(value = "process:data:list")
    @ResponseBody
    public Object attributeList(
            @RequestParam(value = "id", required = true) String id
    ) throws Exception {
        List<TProcessAttribute> processAttributes = processManageService.findSearchAttribute(id);
        return JsonFilter.doPropertyFilter(processAttributes, new PropertyCollection()
                .addProperties(TProcessAttribute.class, "id", "fieldName", "fieldType", "unit", "fieldTypeParam"));
    }
}
